﻿using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;

public partial class ProductPCBAging_ManagePCBAging_PCBAgingAdd : System.Web.UI.Page
{
    DataTable dt;
    PCBAging ag;
    AgingItem agi;
    PCBAgingInfo paginfo;
    //public static string pCBAgingID;

    public ProductPCBAging_ManagePCBAging_PCBAgingAdd()
    {
        dt = new DataTable();
        ag = new PCBAging();
        agi = new AgingItem();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
 
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        PCBAdd();
    }

    protected void TextBox2_TextChanged(object sender, EventArgs e)
    {
        PCBAdd();
    }

    protected void PCBAdd()
    {
        string productSN = this.TextBox2.Text.Trim();
        if (productSN.Length < 10)
        {
            string str1 = "PCB板序列号过短，请检查！";
            //ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str1 + "');</script>");
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (productSN.Length > 50)
        {
            string str1 = "PCB板序列号过长，请检查！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (ProductSNCompare(productSN))
        {
            string str1 = "PCB板序列号已在列表中！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            ListBox1.Items.Insert(0, productSN);
            this.Label1.Text = this.ListBox1.Items.Count.ToString();
            this.TextBox2.Text = "";
            this.TextBox2.Focus();
        }
    }

    protected bool ProductSNCompare(string psn)
    {
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Text == psn)
            {
                return true;
            }
        }
        return false;
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        ListItemCollection lic = new ListItemCollection();
        foreach (ListItem item in this.ListBox1.Items)
        {
            if (item.Selected)
                lic.Add(item);
        }
        foreach (ListItem item in lic)
        {
            this.ListBox1.Items.Remove(item);
        }
        this.Label1.Text = this.ListBox1.Items.Count.ToString(); 
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        this.ListBox1.Items.Clear();
        this.Label1.Text = this.ListBox1.Items.Count.ToString();
    }

    protected void Button4_Click(object sender, EventArgs e)
    {
        string pCBAgingID = this.TextBox1.Text.Trim();
        if (ag.PCBAging_ExistsID(pCBAgingID))
        {
            string str1 = "批次编号已存在！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else if (this.ListBox1.Items.Count == 0)
        {
            string str1 = "请先添加PCB板序列号！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            BiuldDT(pCBAgingID);
            DataInsert(pCBAgingID);
        }
    }

    protected void BiuldDT(string pCBAgingID)
    {
        dt.Columns.Add("PCBAgingID");
        dt.Columns.Add("PCBSN");
        DataRow dr;
        foreach (ListItem item in this.ListBox1.Items)
        {
            dr = dt.NewRow();
            dr["PCBAgingID"] = pCBAgingID;
            dr["PCBSN"] = item.Text;
            dt.Rows.Add(dr);
        }
    }

    protected void DataInsert(string pCBAgingID)
    {
        paginfo = new PCBAgingInfo(pCBAgingID, DateTime.Now, User.Identity.Name, this.TextBox5.Text.Trim(), this.TextBox6.Text.Trim(), dt.Rows.Count);              //修改重点在这一句
        if (ag.PCBAgingAndItem_Insert(paginfo, dt))
        {
            string str1 = "信息插入成功！这一批共有" + paginfo.Amount + "个PCB板。";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        else
        {
            string str1 = "信息插入失败！";
            ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "click", "<script>alert('" + str1 + "');</script>", false);
        }
        dt.Dispose();
    }
}
